package com.lan.ot;


import com.lan.model.Type;
import com.lan.model.Variable;
import com.lan.model.symbol.Function;

import java.util.HashMap;
import java.util.Map;

/**
 * 存放一个函数运行时的本地变量的值，包括参数的值。
 */
public class FunctionOt implements Ot {

    private Map<Variable, Object> variables = new HashMap<>();

    private Map<String, Variable> keyVariable = new HashMap<>();

    /**
     * 为了 json  this. 暂时不需要  只需要单纯的方法  工具方法
     */
    private ClazzOt clazzOt;

    private Function function;

    private Ot rv;

    public FunctionOt(Function function, ClazzOt clazzOt) {
        this.function = function;
        this.clazzOt = clazzOt;
    }

    public FunctionOt(Function function) {
        this.function = function;
    }

    public void setFunction(Function function) {
        this.function = function;
    }

    public Function getFunction() {
        return function;
    }

    public Ot getRv() {
        return rv;
    }

    public void setRv(Ot rv) {
        this.rv = rv;
    }

    @Override
    public Type type() {
        return Type.Function;
    }

    public ClazzOt getClazzOt() {
        return clazzOt;
    }

    public void setClazzOt(ClazzOt clazzOt) {
        this.clazzOt = clazzOt;
    }

    @Override
    public Object getValue(Variable variable) {
        return variables.get(variable);
    }

    @Override
    public Variable getVariable(String name) {
        Variable variable = keyVariable.get(name);
        if (variable == null) {
            variable = function.getVariable(name);
        }
        return variable;
    }

    @Override
    public void setValue(Variable variable, Object value) {
        keyVariable.put(variable.getName(), variable);
        variables.put(variable, value);
    }
}